package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.web.dao.CommonDao;
import com.ys.web.entity.Account;
import com.ys.web.entity.SemPlatformType;
import com.ys.web.pojo.AccountInfo;
import com.ys.web.remotemodel.RemoteAccountInfo;
import com.ys.web.service.AccountService;
import com.ys.web.ws.service.ManageCompanyInfoService;

@Service
@Transactional
public class AccountServiceImpl implements AccountService{

	@Autowired
	private CommonDao commonDao;
	
	@Resource
	private ManageCompanyInfoService manageCompanyInfoService;
	@Override
	public Integer addAccount(AccountInfo accountInfo) {
		Account account = new Account();
		
		
		BeanUtils.copyProperties(accountInfo, account);
		if(accountInfo!=null){
			if("1".equals(accountInfo.getAccountState())){
				account.setAccountState((byte)1);
			}else{
				account.setAccountState((byte)0);
			}
			
		}
		
		return (Integer) commonDao.save(account);
	}

	@Override
	public void saveAccount(AccountInfo accountInfo) {
		Account account = new Account();
		BeanUtils.copyProperties(accountInfo, account);
		if(accountInfo!=null){
			if("1".equals(accountInfo.getAccountState())){
				account.setAccountState((byte)1);
			}else{
				account.setAccountState((byte)0);
			}
			
		}
		
		commonDao.saveOrUpdate(account);
	}

	
	@Override
	public List<AccountInfo> getAccountByCompanyId(Integer companyId) {
		List<Account> accounts = commonDao.findByProperty(Account.class, "companyId", companyId);
		List<AccountInfo> accountInfos = new ArrayList<AccountInfo>();
		
		for(Account account:accounts){
			AccountInfo accountInfo = new AccountInfo();
			
			BeanUtils.copyProperties(account, accountInfo);
			SemPlatformType semPlatformType = commonDao.
					findUniqueByProperty(SemPlatformType.class, "typeTag", account.getTypeTag());
			if(semPlatformType!=null){
				accountInfo.setSemPlatformName(semPlatformType.getTypeName());
				accountInfo.setTypeTag(semPlatformType.getTypeTag());
			}
			
			if((int)account.getAccountState()==1){
				accountInfo.setAccountState("1");
			}else{
				accountInfo.setAccountState("0");
			}
			
			
			accountInfos.add(accountInfo);
		}
		return accountInfos;
	}	
	
	/**
	 * 根据公司id获取账户信息
	 * @param companyId
	 * @return
	 */
	@Override
	public List<RemoteAccountInfo> getAccountByCompanyKADId(Integer companyId){
		return manageCompanyInfoService.getAccountByCompanyId(companyId);
	}

	

	@Override
	public AccountInfo getAccountById(Integer id) {
		AccountInfo accountInfo = new AccountInfo();
		Account account = commonDao.get(Account.class, id);
		BeanUtils.copyProperties(account, accountInfo);
		if(account.getAccountState()==(byte)1){
			accountInfo.setAccountState("1");
		}else{
			accountInfo.setAccountState("0");
		}
		SemPlatformType semPlatformType = commonDao.
				findUniqueByProperty(SemPlatformType.class, "typeTag", account.getTypeTag());
		accountInfo.setSemPlatformName(semPlatformType.getTypeName());
		return accountInfo;
	}

	@Override
	public void deleteAccount(Integer id) {
		Account account = commonDao.get(Account.class, id);
		commonDao.delete(account);
		
	}

	@Override
	public Account getAccountByAccountName(String accountName) {
		Account account = commonDao.findUniqueByProperty(Account.class, "loginName", accountName);
		return account;
	}

	@Override
	public Boolean isAccountExist(String accountName) {
		// TODO Auto-generated method stub
		Account account = getAccountByAccountName(accountName);
		if(account==null){
			return false;
		}
		return true;
	}

	@Override
	public Boolean isAccountExist(AccountInfo accountInfo) {
		String hql = "from Account where typeTag=? and loginName=?";
		List<Account> accounts =  commonDao.findHql(hql, accountInfo.getTypeTag(),accountInfo.getLoginName());
		if(accounts==null||accounts.size()==0){
			return false;
		}
		return true;
	}

	@Override
	public Boolean isHasSameAccount(AccountInfo accountInfo) {
		String hql = "from Account where typeTag=? and loginName=? and id <> ?";
		List<Account> accounts =  commonDao.findHql(hql, accountInfo.getTypeTag(),accountInfo.getLoginName(),accountInfo.getId());
		if(accounts==null||accounts.size()==0){
			return false;
		}
		return true;
	}

	/**
	 * 删除账户
	 */
	@Override
	public void deleteAccount(Account account) {
			commonDao.delete(account);
		
	}

	@Override
	public Boolean isOtherUserHasThisAccount(AccountInfo accountInfo) {
		// TODO Auto-generated method stub
		return null;
	}


	/**
	 * 更新账号、CPC、
	 * @param remoteAccountInfo
	 */
	public void updateRemoteAccountInfo(RemoteAccountInfo remoteAccountInfo) {
		manageCompanyInfoService.updateRemoteAccountInfo(remoteAccountInfo);	
	}

}
